require(['select2', 'date', 'validator', 'grid'], function () {
    var cnt = 0,
        $holidayListForm = $("#holidayListForm"),
        $setHolidayTab = $("#setHolidayTab");

    $holidayListForm.initForm();

    add = function(fn) {
        addOrUpdate(fn);
    };

    reset = function (){
        $holidayListForm.reset($holidayListForm);
    };


    function addOrUpdate(callBack){
        var result = $.validator($holidayListForm);
        if (result) {
            $.messager.confirm("是否保存", "是否设定这些节假日？", function (r) {
                if (r) {
                    $.ajaxExt({
                        url: "/holidayParam/setHolidays.htm",
                        data: $holidayListForm.serialize()
                    }).done(function (data) {
                        $.messager.alert("提示", "设定节假日成功。", "info");
                        callBack();
                    });
                }
            });
        }
    }
    $setHolidayTab.on("click.addRowA", "#addRowA", function () { // 添加行的点击事件
        cnt++;
        var $endTR = $("#endTR");
        var newTR = "<tr id='newTR'>"
            + "<td style='border-left: 1px solid #DCDCDC' class='ac'><select dict='HOLI0000' name='holidays' class='u-input' data-validator='{required:true}' style='width:100px'><option value=''>请选择</option></select></td>"
            + "<td class='ac'><div class='select-box select-box-large'><input type='text' id='add_beginDate" + cnt + "' name='beginDates' data-validator='{required:true}' class='u-input' style='width:100px' onclick=\"WdatePicker({isShowClear:true,dateFmt:'yyyy-MM-dd'})\"><span class='icon-date'></span></div></td>"
            + "<td class='ac'><div class='select-box select-box-large'><input type='text' id='add_endDate" + cnt + "' name='endDates' data-validator=\"{dateCompare:'add_beginDate" + cnt + "',required:true}\" class='u-input' style='width:100px' onclick=\"WdatePicker({isShowClear:true,dateFmt:'yyyy-MM-dd'})\"><span class='icon-date'></span></div></td>"
            + "<td class='ac'><select name='workDayInds' class='u-input' style='width:100px;' data-validator='{required:true}'><option value='0'>非工作日</option><option value='1'>工作日</option></select></td>"
            + "<td class='ac'><a class='operate operate-delete delRowA' href='javascript:;'>删除</a></td>"
            + "</tr>";
        $endTR.after(newTR);
        // 初始化下拉框数据
        // 切换id
        var $newTR = $("#newTR");
        $endTR.removeAttr("id");
        $newTR.attr({id: "endTR"});
        $holidayListForm.initForm();
    }).on("click.delRowA", ".delRowA", function () { // 删除行的点击事件
        if ($("#setHolidayTab tr").length == 3) {
            $.messager.alert("提示", "这是最后一条信息了，请不要删除。", "info");
        } else {
            var delTR = $(this).closest('tr');
            if (delTR.attr("id") === "endTR") {
                delTR.prev().attr({id: "endTR"});
            }
            delTR.remove();
        }
    });
});